package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.ShopRepository;
import com.qf.pojo.BaseResult;
import com.qf.pojo.TbShop;
import com.qf.pojo.TbShopVo;
import com.qf.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created by 54110 on 2021/5/11.
 */
@Service
public class ShopCartServiceImpl implements ShopCartService{

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ShopRepository shopRepository;

    @Override
    public BaseResult add(Integer shopid, String userid) {



            //1.第二层key为shopid 验证该购物车中是否拥有该商品
            Object o = redisTemplate.opsForHash().get("shopcart_" + userid.toString(), shopid.toString());
            if (o!=null){
                String string = JSONObject.toJSONString(o);
                TbShopVo tbShopVo = JSONObject.parseObject(string, TbShopVo.class);
                Integer num = tbShopVo.getNum();
                tbShopVo.setNum(num+1);
                redisTemplate.opsForHash().put("shopcart_"+userid,shopid.toString(),tbShopVo);
                return new BaseResult(0,"ok");
            }else{
                //使用shopid去数据库查询 出该商品的信息
                Optional<TbShop> byId = shopRepository.findById(shopid);
                //将查询出来的bean对象copy到存储到redis中的对象中
                TbShop tbShop = byId.get();
                TbShopVo tbShopVo = new TbShopVo();
                tbShopVo.setNum(1);
                BeanUtils.copyProperties(tbShop,tbShopVo);
                redisTemplate.opsForHash().put("shopcart_"+userid.toString(),shopid.toString(),tbShopVo);
                return new BaseResult(0,"ok");
            }
    }

    @Override
    public BaseResult del(Integer shopid, String userid) {
        Boolean aBoolean = redisTemplate.hasKey("shopcart_"+ userid);
        if (aBoolean){

            Object o = redisTemplate.opsForHash().get("shopcart_" + userid, shopid.toString());
            String string = JSONObject.toJSONString(o);
            TbShopVo tbShopVo = JSONObject.parseObject(string, TbShopVo.class);
            Integer num = tbShopVo.getNum();
            if (num>1){
                tbShopVo.setNum(num-1);
                redisTemplate.opsForHash().put("shopcart_" + userid, shopid.toString(),tbShopVo);
                return new BaseResult(0,"ok");
            }else{
                //删除的时候第一层key为用户的id 第二层key为商品id
                redisTemplate.opsForHash().delete("shopcart_"+userid,shopid.toString());
                return new BaseResult(0,"ok");
            }


        }else{
            return new BaseResult(1,"购物车没有商品！请先添加商品");
        }
    }

    @Override
    public BaseResult findShopCart( String userid) {

        Boolean aBoolean = redisTemplate.hasKey("shopcart_" + userid);
        if (aBoolean){

            List values = redisTemplate.opsForHash().values("shopcart_" + userid);
            return new BaseResult(0,"ok",values);

        }else{
            return new BaseResult(1,"购物车没有商品！请先添加商品");
        }


    }
}
